﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;
using System.IO;

using WCFFormClient.MP3OpeatorServiceReference;

namespace WCFFormClient
{
    public partial class Form1 : Form
    {
        SoundPlayer player = new SoundPlayer();
        MP3OpeatorClient client = new MP3OpeatorClient("NetTcpBinding_IMP3Opeator");
        string[] list;
        public Form1()
        {
            InitializeComponent();
            list = client.GetMusicList().Select(m=>m.Name).ToArray();
            if(list.Count() > 0)
            {
                this.musicListBox.DataSource = list;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void musicListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            player.Stop();
            try
            {
                player.Stream = client.GetMusicSream(this.musicListBox.Text);
                player.Play();
            }
            catch (FaultException<FileNotFoundException> ex)
            {
                MessageBox.Show(ex.Detail.InnerException.Message);
            }
            catch (CommunicationException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void flushBtn_Click(object sender, EventArgs e)
        {
            list = client.GetMusicList().Select(m => m.Name).ToArray();
            if (list.Count() > 0)
            {
                this.musicListBox.DataSource = list;
            }
        }

        private void playBtn_Click(object sender, EventArgs e)
        {
            player.Play();
        }

        private void paueBtn_Click(object sender, EventArgs e)
        {
            player.Stop();
        }

        private void ReBtn_Click(object sender, EventArgs e)
        {
        }

    }
}
